﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BestCode.Creds.Business;
using BestCode.Creds.Entities;
using BestCode.Wpf.ViewModel.Data;

namespace BestCode.Creds.UI.ViewModel
{
    public class VmEntityChild : DataModelBase<EntityChild>
    {
        EntityChildBusiness business = new EntityChildBusiness();
        

        private ObservableCollection<EntityChild> childrenByEntity;
        public ObservableCollection<EntityChild> ChildrenByEntity
        {
            get { return childrenByEntity; }
            set { childrenByEntity = value; this.FirePropertyChanged("ChildrenByEntity"); }
        }


        public EntityInfo EntityParent { get; set; }

        public VmEntityChild()
        {
            SetDataService(business);
            SelectedItemChanged += VmEntityChild_SelectedItemChanged;
        }

        void VmEntityChild_SelectedItemChanged(object sender, EventArgs e)
        {
            if (SelectedItem != null && EntityParent != null)
                SelectedItem.Entity = EntityParent;
        }

        public void LoadByEntityId(int entityId)
        {
            ChildrenByEntity = new ObservableCollection<EntityChild>(business.GetByEntityId(entityId));
            Items = ChildrenByEntity;
        }

        public void LoadByEntityId(EntityInfo entity)
        {
            ChildrenByEntity = new ObservableCollection<EntityChild>(business.GetByEntityId(entity.Id));
            EntityParent = entity;
            Items = ChildrenByEntity;
        }
        


    }
}
